package labox.innovation.gameserver.network.clientpackets;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import labox.innovation.config.Config;
import labox.innovation.gameserver.GameTimeController;
import labox.innovation.gameserver.model.Punish;
import labox.innovation.gameserver.model.Scene;
import labox.innovation.gameserver.model.FuzeSkillCoolDown.TimeStamp;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance.KarmaStat;
import labox.innovation.gameserver.network.serverpackets.MagicSkillUse;

/**
 * 请求复活逻辑的处理
 * 
 * @version $Revision: 1.7.2.3.2.6 $ $Date: 2005/03/27 15:29:30 $
 */
public final class RequestRestartPoint extends L2GameClientPacket {

	private static final String _C__53_REQUESTRESTARTPOINT = "[C] 53 RequestRestartPoint";

	private static final Logger _log = LoggerFactory.getLogger(RequestRestartPoint.class.getName());

	private static final int TOWN_RESTART = 0x00;//

	private static final int SITE_RESTART = 0x01;

	private static final int JAIL_RESTART = 0x02;

	protected int _requestedPointType;

	@Override
	protected void readImpl() {
		_requestedPointType = readUnByte();
	}

	@Override
	protected void runImpl() {
		FuzePcInstance activeChar = getClient().getActiveChar();

		if (activeChar == null)
			return;
		if (!activeChar.isDead()) {
			_log.warn("Living player [" + activeChar.getName() + "] called RestartPointPacket! Ban this player!", activeChar.getObjectId(), activeChar.getName());
			return;
		}

		int id = 0, x = 0, y = 0;

		if (_requestedPointType != SITE_RESTART && (activeChar.getPunish().isInJail() || activeChar.getKarmaStat() == KarmaStat.EVIL)) {// force jail
			_requestedPointType = JAIL_RESTART;
		}

		Scene s = activeChar.getScene();
		if (s == null) {//应该不会有空场景
			_log.warn("player RequestRestartPoint but he in null scene!", Config.SERVER_ID, "场景");
			return;
		}

		activeChar.setIsReviveFullResume(false);
		// Teleport and revive
		activeChar.setInstanceId(0);
		activeChar.setIsPendingRevive(true);
		switch (_requestedPointType) {
		case SITE_RESTART:
			int pre_time = activeChar.getPreviousSituReviveTime();
			if (GameTimeController.getGameTicks() - pre_time > 600) {
				activeChar.setSituReviveCost(Config.RESPAWN_COST);
			} else {
				activeChar.setSituReviveCost(activeChar.getSituReviveCost() * 2);
			}

			if (activeChar.reduceMoneyNote("银票复活", activeChar.getSituReviveCost(), null, true)) {
				id = s.get_id();
				x = activeChar.getX();
				y = activeChar.getY();
				activeChar.setIsReviveFullResume(true);
				activeChar.setPreviousSituReviveTime(GameTimeController.getGameTicks());
				break;
			}
		case TOWN_RESTART:
			switch (activeChar.getRace()) {
			case HUMAN:
				id = s.getHumanReviveScene();
				x = s.getHumanReviveX();
				y = s.getHumanReviveY();
				break;
			case ELF:
				id = s.getELFReviveScene();
				x = s.getELFReviveX();
				y = s.getELFReviveY();
				break;
			}
			break;
		case JAIL_RESTART:
			activeChar.getPunish().setPunishLevel(Punish.PunishLevel.JAIL, 0);
			return;
		default:
			return;
		}

		activeChar.teleToLocation(id, x, y);

		//发送技能冷却数据
		for (TimeStamp ts : activeChar.getSkillCoolList().getSkillTimeStamps().values()) {
			if (ts != null) {
				sendPacket(new MagicSkillUse(ts.getSkill(), ts.getItemId(), (int) ts.getRemaining(), 0, (int) activeChar.getSkillCoolList().getSkillCatCoolTimeById(ts.getSkill())));
			}
		}
	}

	/* (non-Javadoc)
	* @see labox.innovation.gameserver.clientpackets.ClientBasePacket#getType()
	*/
	@Override
	public String getType() {
		return _C__53_REQUESTRESTARTPOINT;
	}
}
